/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is Tamina Framework.
  
  The Initial Developer of the Original Code is
  MOUTON David (aka damo)  <damo@tamina-online.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package org.tamina.utils.preload
{	
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.utils.setInterval;
	
	import org.tamina.component.SimpleProgressBar;
	import org.tamina.core.init.InitEvent;

	public class SuperPreloader extends Sprite 
	{
		//Props
		private var label:TextField;
		private var progressionTxt:TextField;
		public var bg:Shape;
		public var progressBar:SimpleProgressBar;
		private static var __applicationLogo:String="appLogo.gif";
		private static var __applicationName:String="Application";
		private static var __applicationStyles:String="preloaderStyles.css";
		
	    private var __width:uint=300; 
	    private var __height:uint=200;	
	    private const LOGO_MAX_WIDTH:uint=80;	
	    private const LOGO_MAX_HEIGHT:uint=80;
		private var __bgColorTop:uint=0xFFFFFF;
		private var __bgColorBottom:uint=0x999999;
		private var __bgOpacity:uint=100;	
		private var __borderColor:uint=0x333333;
		private var __borderOpacity:uint=100;
		private var __borderSize:uint=3;
		private var __borderRange:uint=16;
		private var __gradientAngle:uint=90;
		private var __gradientType:String=GradientType.LINEAR;	
		private var __hasShadow:Boolean=true;
		private var __PreloaderStyles:StyleSheet;
		private var __cssLoader:URLLoader;
        
        //Contructeur
        public function SuperPreloader()
        {
        	super();
	        drawBg();
	        //drawLogo();
	        drawText();	        
	        drawProgressBar();              
        }
        
        //Public methode     
        /**
         * Defini la valeur du titre
         * @param val
         * 
         */        
        public function setTitle(val:String):void{
        	__applicationName = val;
        	label.htmlText = "<span class='preloader-title'>"+val+"</span>";
        }
        /**
         * Defini la valeur du texte d'info
         * @param val
         * 
         */        
        public function setInfo(val:String):void{
        	progressionTxt.htmlText = "<span class='preloader-info'>"+val+"</span>";
        }
        public function setLogo(url:String):void{
			var l:Loader = new Loader();
			var urlReq:URLRequest = new URLRequest(url);
			l.contentLoaderInfo.addEventListener(Event.INIT, resizeLogo);
			l.load(urlReq);
			l.x = 10;
			l.y = 10;
			addChild(l);
		}
		public function setStyle(url:String):void{
			__PreloaderStyles = new StyleSheet();
        	__cssLoader = new URLLoader();
        	__cssLoader.addEventListener(Event.COMPLETE,drawStyles);
        	__cssLoader.load(new URLRequest(url));
		}
        //Private
        private function drawStyles(e:Event):void{
        	__cssLoader.removeEventListener(Event.COMPLETE,drawStyles);
        	__PreloaderStyles.parseCSS(__cssLoader.data);
        	label.styleSheet = __PreloaderStyles;
        	setTitle(__applicationName);
        	progressionTxt.styleSheet = __PreloaderStyles;
        }
        private function getMatrix():Matrix{
        	var result:Matrix = new Matrix();
        	result.createGradientBox(1,__height,(__gradientAngle/180)*Math.PI);
			return result;
		} 
		private function drawBg():void{
			bg = new Shape();
            bg.graphics.clear();
            bg.graphics.lineStyle(__borderSize,__borderColor,__borderOpacity);
			bg.graphics.beginGradientFill(__gradientType,[__bgColorTop,__bgColorBottom],[__bgOpacity,__bgOpacity],[0,255],getMatrix());
			bg.graphics.drawRoundRect(0,0,__width,__height,__borderRange);
			bg.graphics.endFill();
			addChild(bg);
			if(__hasShadow){
				var filter:DropShadowFilter = new DropShadowFilter(9, 45, 0x000000, 0.5, 10, 10, 1, 3, false, false, false);
				var filterArray:Array = new Array();
				filterArray.push(filter);
				bg.filters = filterArray;
			} else {
				bg.filters = [];
			}
		}
		
		private function resizeLogo(e:Event):void{			
			var li:LoaderInfo = LoaderInfo(e.currentTarget);
			li.removeEventListener(Event.INIT, resizeLogo);
			var scaleValue:uint=100;
			if(li.content.width >= li.content.height){
				scaleValue = LOGO_MAX_WIDTH/li.content.width;
			} else {
				scaleValue = LOGO_MAX_HEIGHT/li.content.height;
			}
			li.content.scaleX = scaleValue;
			li.content.scaleY = scaleValue;
		}
		private function drawText():void{
			//AppName
			label = new TextField();
			label.x = 98;
			label.y = 66;
			label.width = width - label.x
			setTitle(__applicationName);
			addChild(label);
			//ProgressTxt
			progressionTxt = new TextField();
			progressionTxt.x = label.x;
			progressionTxt.y = label.y + 23;
			progressionTxt.width = width - progressionTxt.x;
			setInfo("Chargement de ...");
			addChild(progressionTxt);
		}
		private function drawProgressBar():void{
			progressBar = new SimpleProgressBar();
			progressBar.x = 10;
			progressBar.y = 125;
            addChild(progressBar);
		}
		 
	}
}